package com.partner.service.member;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.partner.bean.member.Member;
import com.partner.bean.member.MemberVo;
import com.partner.bean.member.Tx;
import com.partner.mapper.member.MemberMapper;

@Service("member")
public class MemberService {

	@Autowired
	private MemberMapper memberMapper;
	java.text.DateFormat sdf = new java.text.SimpleDateFormat(
			"yyyy-MM-dd");
	
	public MemberMapper getServiceMapper() {
		return memberMapper;
	}
	
	public int insert(Member s) {
//		int id =0 ;	
		try {
		   memberMapper.insert(s);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return s.getId() ;
	}
	
	public List<Member> selectAll(Member member) {
		return memberMapper.selectAll(member);
	}
	
	public Member selectByPrimaryId(Integer id){
		return memberMapper.selectByPrimaryId(id);
	}
	
	public int removeByPrimaryKey(Integer id){
		return memberMapper.deleteByPrimaryKey(id);
	}
	
	public int deleteByids(String[] ids){
		return memberMapper.deleteByids(ids);
	}
	
	public int updateByPrimaryKey(Member member){
		return memberMapper.updateByPrimaryKey(member);
	}

	public List<Member> getMembernum(Member member) {
		return memberMapper.getMembernum(member);
	}

	public Integer selectdirectnum(Integer pid) {
		return memberMapper.selectdirectnum(pid);
	}
	
	public List<Member> selectnum() {
		return memberMapper.selectnum();
	}
	
	public Integer getMaxNum() {
		return memberMapper.getMaxNum();
	}
	
	public Member selectByNum(Integer num){
		return memberMapper.selectByNum(num);
	}

	public void updateBatch(List<Member> memList) {
		memberMapper.updateBatch(memList);
	}

	public Integer selectLevelMemberCount() {
		return memberMapper.selectLevelMemberCount();
	}
	
	public List<Member> selectLevelMember(Integer[] level) {
		return memberMapper.selectLevelMember(level);
	}

	public List<MemberVo> forMatterTx(List<Member> memberList) {
		List<MemberVo> list1 = new ArrayList<>();
		if(memberList!=null&&memberList.size()>0){
			Member member = memberList.get(0);
			MemberVo tx = new MemberVo();
			tx.setId(member.getId());
			tx.setName(member.getName());
			tx.setPid(member.getPid());
			tx.setCreatetime(member.getCreatetime());
			tx.setNum(member.getNum());
			tx.setTel(member.getTel());
			if(member.getSex()!=null&&!member.getSex().equals("")){
				String sexname = ""; 
				if(member.getSex()==1){
					sexname = "男";
				}else{
					sexname = "女";
				}
				tx.setSexname(sexname);
			}
			if(member.getLevel()!=null&&!member.getLevel().equals("")){
				String levelname = ""; 
				if(member.getLevel()==1){
					levelname = "天使合伙人";
				}else if(member.getLevel()==2){
					levelname = "总监";
				}else if(member.getLevel()==3){
					levelname = "名誉总监";
				}else if(member.getLevel()==4){
					levelname = "督导";
				}else if(member.getLevel()==5){
					levelname = "名誉督导";
				}else if(member.getLevel()==6){
					levelname = "董事";
				}else if(member.getLevel()==7){
					levelname = "名誉董事";
				}
				tx.setLevelname(levelname);
			}
			tx.setPname(member.getPname());
			tx.setPtel(member.getPtel());
			tx.setTimematter(sdf.format(member.getCreatetime()));
			List<MemberVo> childrens = getChildrens(memberList,member.getId());
			tx.setChildrens(childrens);
			list1.add(tx);
		}
		
		return list1;
	}

	private List<MemberVo> getChildrens(List<Member> memberList,int id) {
		List<MemberVo> list2 = new ArrayList<>();
		for(int i=0 ;i<memberList.size() ; i++){
			Member member = memberList.get(i);
			if(member!=null&&member.getPid()!=null){
				int pid = member.getPid();
				if(pid==id){
					MemberVo tx = new MemberVo();
					tx.setId(member.getId());
					tx.setName(member.getName());
					tx.setPid(member.getPid());
					tx.setCreatetime(member.getCreatetime());
					tx.setNum(member.getNum());
					tx.setTel(member.getTel());
					if(member.getSex()!=null&&!member.getSex().equals("")){
						String sexname = ""; 
						if(member.getSex()==1){
							sexname = "男";
						}else{
							sexname = "女";
						}
						tx.setSexname(sexname);
					}
					if(member.getLevel()!=null&&!member.getLevel().equals("")){
						String levelname = ""; 
						if(member.getLevel()==1){
							levelname = "天使合伙人";
						}else if(member.getLevel()==2){
							levelname = "总监";
						}else if(member.getLevel()==3){
							levelname = "名誉总监";
						}else if(member.getLevel()==4){
							levelname = "督导";
						}else if(member.getLevel()==5){
							levelname = "名誉督导";
						}else if(member.getLevel()==6){
							levelname = "董事";
						}else if(member.getLevel()==7){
							levelname = "名誉董事";
						}
						tx.setLevelname(levelname);
					}
					tx.setPname(member.getPname());
					tx.setPtel(member.getPtel());
					tx.setTimematter(sdf.format(member.getCreatetime()));
					List<MemberVo> childrens = getChildrens(memberList,member.getId());
					tx.setChildrens(childrens);
					list2.add(tx);
				}
			}
		}
		return list2;
	}
}
